package com.cmc.cloud.cmclink.doc.service.impl.manifest;

import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.ManifestCntrErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.ManifestCntrConvert;
import com.cmc.cloud.cmclink.doc.entity.ManifestCntrDO;
import com.cmc.cloud.cmclink.doc.mapper.ManifestCntrMapper;
import com.cmc.cloud.cmclink.doc.service.manifest.ManifestCntrService;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifestcntr.ManifestCntrCreateReqVO;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifestcntr.ManifestCntrRespVO;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifestcntr.ManifestCntrUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 舱单箱信息 Service 实现类
 *
 * @author chenzheng
 */
@Service
@Validated
public class ManifestCntrServiceImpl implements ManifestCntrService {

    @Resource
    private ManifestCntrMapper manifestCntrMapper;

    @Override
    public void createManifestCntr(ManifestCntrCreateReqVO createReqVO) {
        // 插入
        ManifestCntrDO manifestCntr = ManifestCntrConvert.INSTANCE.convert(createReqVO);
        manifestCntrMapper.insert(manifestCntr);
    }

    @Override
    public void updateManifestCntr(ManifestCntrUpdateReqVO updateReqVO) {
        // 校验存在
        validateManifestCntrExists(updateReqVO.getId());
        // 更新
        ManifestCntrDO updateObj = ManifestCntrConvert.INSTANCE.convert(updateReqVO);
        manifestCntrMapper.updateById(updateObj);
    }

    @Override
    public void deleteManifestCntr(Long id) {
        // 校验存在
        validateManifestCntrExists(id);
        // 删除
        manifestCntrMapper.deleteById(id);
    }

    private void validateManifestCntrExists(Long id) {
        if (manifestCntrMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(ManifestCntrErrorCodeConstants.MANIFEST_CNTR_NOT_EXISTS);
        }
    }

    @Override
    public ManifestCntrDO getManifestCntr(Long id) {
        return manifestCntrMapper.selectById(id);
    }

    @Override
    public List<ManifestCntrDO> getManifestCntrList(Collection<Long> ids) {
        return manifestCntrMapper.selectBatchIds(ids);
    }

    @Override
    public void createOrUpdateAndDeleteBatch(Long manifestId, List<ManifestCntrUpdateReqVO> newList, List<ManifestCntrRespVO> oldList) {
        // 无效数据直接返回
        if (CollectionUtil.isEmpty(newList) && CollectionUtil.isEmpty(oldList)) {
            return;
        }
        // 默认值
        if (CollectionUtil.isEmpty(newList)) {
            newList = new ArrayList<>();
        }
        if (CollectionUtil.isEmpty(oldList)) {
            oldList = new ArrayList<>();
        }
        List<ManifestCntrUpdateReqVO> createList = new ArrayList<>();
        List<ManifestCntrUpdateReqVO> updateList = new ArrayList<>();
        // 历史数据
        Set<Long> oldIds = oldList.stream().map(e -> e.getId()).collect(Collectors.toSet());
        newList.forEach(e -> {
            if (e.getId() != null) {
                if (oldIds.contains(e.getId())) {
                    // 正常更新
                    updateList.add(e);
                    oldIds.remove(e.getId()); // 剩下的数据需要删除
                } else {
                    // 数据更新非法
                    throw ServiceExceptionUtil.exception(ManifestCntrErrorCodeConstants.MANIFEST_CNTR_NOT_EXISTS);
                }
            } else {
                e.setManifestId(manifestId);
                createList.add(e);
            }
        });
        if (CollectionUtil.isNotEmpty(oldIds)) {
            // 没有匹配上的就是前端删除的
            manifestCntrMapper.deleteBatchIds(oldIds);
        }
        if (CollectionUtil.isNotEmpty(createList)) {
            List<ManifestCntrDO> manifestCntrCreateList = ManifestCntrConvert.INSTANCE.convertUpdateList(createList);
            manifestCntrMapper.insertBatch(manifestCntrCreateList);
        }
        if (CollectionUtil.isNotEmpty(updateList)) {
            List<ManifestCntrDO> manifestCntrUpdateList = ManifestCntrConvert.INSTANCE.convertUpdateList(updateList);
            manifestCntrMapper.updateBatch(manifestCntrUpdateList);
        }
    }
}
